import { defineStore } from "pinia"
import { reactive } from "vue"

export const useStorageUserDataStore = defineStore("storageuserdata", () => {
  const onLogin = reactive({
    userId: "",
    username: "",
    userType: "",
  })

  function Persistence() {
    const onUser = [
      onLogin.userId,
      onLogin.username,
      onLogin.userType,
    ]
    window.localStorage.setItem("onUser", JSON.stringify(onUser))
    // 防止onUser被修改
    window.addEventListener('storage', function () {
      if (e.key == 'onUser') { localStorage.setItem(e.key, e.oldValue) }
    });
  }
  return { onLogin, Persistence }
})
